<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class admin_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    function menu() {
        $this->load->library('menus');
        $menu = new menus;
        $data['menu'] = $menu->show_menu();

        return $data;
    }

    function login($username, $password) {
        $this->db->where("uname", $username);
        $this->db->where("pword", $password);

        $query = $this->db->get('tbl_user');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $rows) {
                $newdata = array(
                    'uid' => $rows->userid,
                    'uname' => $rows->uname,
                    'password' => $rows->pword,
                    'logged_in' => TRUE
                );
            }
            $this->session->set_userdata($newdata);
            return true;
        }
        return false;
    }

    function get_all_user() {
        $this->db->select('*');
        $query = $this->db->from('tbl_user');
        $this->db->join('tbl_role', 'tbl_user.role = tbl_role.roleID');
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_user($userid) {
        $query = $this->db->get_where('tbl_user', array('userid' => $userid));
        return $query->row_array();
    }

    public function insert_user($data) {
        $this->db->insert('tbl_user', $data);

        $id = $this->db->insert_id();

        return (isset($id)) ? $id : FALSE;
    }

    public function update_user($userid, $data) {
        $this->db->where('userid', $userid);

        return $this->db->update('tbl_user', $data);
    }

    function get_role() {
        $this->db->select('*');
        $query = $this->db->get('tbl_role');

        return $query->result_array();
    }

    public function insert_rank($data) {
        $this->db->insert('tbl_user_rank', $data);

        $id = $this->db->insert();
    }

}
